<template>
  <div class="tree-item">
    <span class="i1">{{ item.name }}</span>
    <span class="i2">{{ item.manager }}</span>
    <el-dropdown class="is" @command="commandEvent">
      <span>操作</span>
      <i class="el-icon-arrow-down el-icon--right" />
      <template>
        <el-dropdown-menu>
          <el-dropdown-item command="add"> 添加子部门 </el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="look">
            查看部门
          </el-dropdown-item>
          <el-dropdown-item v-if="!item.isTop" command="del">
            删除部门
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
// 导入删除部门api
import { delDepartment } from '@/api/department'
export default {
  name: 'TreeItem',
  props: {
    item: {
      type: Object,
      default: () => {}
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('changeShow', true, this.item) // 传递item
          break
        case 'del':
          // console.log(this.item.id)
          this.$confirm('确定要删除该部门吗', '提示')
            .then(async() => {
              // 如果点击了确定就会进入then
              await delDepartment(this.item.id) // 返回promise对象
              //  如果删除成功了  就会进入这里
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch(() => {})
          break
        case 'look':
          this.$bus.$emit('changeShow', true, this.item, 'edit')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tree-item {
  display: flex;
  align-items: center;
  .i1 {
    flex: 1;
  }
  .i2 {
    width: 150px;
    padding-right: 50px;
    text-align: right;
  }
  .i3 {
    width: 150px;
    text-align: right;
  }
}
</style>
